import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ugrownews_flutter/utils/MD5Util.dart';
import 'package:ugrownews_flutter/utils/Toast.dart';
import 'package:ugrownews_flutter/utils/netutil/LoginNetUtil.dart';

class ScreenLogin extends StatefulWidget {
  @override
  _ScreenLoginState createState() => _ScreenLoginState();
}

class _ScreenLoginState extends State<ScreenLogin> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        height: ScreenUtil.screenHeightDp,
        width: ScreenUtil.screenWidthDp,
        child: new Container(
          color: Colors.blueAccent,
          child: new Stack(
            children: <Widget>[
              new Positioned(
                height: ScreenUtil.instance.setWidth(200),
                width: ScreenUtil.screenWidthDp,
                top: ScreenUtil.instance.setWidth(200),
                child: new Center(
                  child: new Image.asset("images/icon.png"),
                ),
              ),
              new Positioned(
                height: ScreenUtil.instance.setWidth(750),
                width: ScreenUtil.screenWidthDp,
                top: ScreenUtil.instance.setWidth(400),
                child: new Center(
                  child: new MineLoginUsername(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class MineLoginUsername extends StatefulWidget {
  @override
  _MineLoginUsernameState createState() => _MineLoginUsernameState();
}

class _MineLoginUsernameState extends State<MineLoginUsername> {
  TextEditingController _controllerUsername;
  TextEditingController _controllerPassword;

  @override
  void initState() {
    _controllerUsername = new TextEditingController();
    _controllerPassword = new TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Stack(
      alignment: Alignment.topCenter,
      children: <Widget>[
        new Positioned(
          left: 0,
          child: new GestureDetector(
            onTap: () {},
            child: new Container(
              color: Colors.yellow,
            ),
          ),
          width: ScreenUtil.screenWidthDp / 2,
          height: ScreenUtil.instance.setWidth(150),
        ),
        new Positioned(
          left: ScreenUtil.screenWidthDp / 2,
          child: new GestureDetector(
            onTap: () {},
            child: new Container(
              color: Colors.red,
            ),
          ),
          width: ScreenUtil.screenWidthDp / 2,
          height: ScreenUtil.instance.setWidth(150),
        ),
        new Positioned(
          top: ScreenUtil.instance.setWidth(180),
          child: new Container(
            color: Colors.white,
            width: ScreenUtil.screenWidthDp,
            height: ScreenUtil.instance.setWidth(200),
            child: new TextField(
              controller: _controllerUsername,
              keyboardType: TextInputType.text,
              decoration: new InputDecoration(
                border: InputBorder.none,
                hintText: "用户名",
              ),
            ),
          ),
        ),
        new Positioned(
          top: ScreenUtil.instance.setWidth(380),
          child: new Container(
            color: Colors.white,
            width: ScreenUtil.screenWidthDp,
            height: ScreenUtil.instance.setWidth(200),
            child: new TextField(
              controller: _controllerPassword,
              keyboardType: TextInputType.text,
              decoration: new InputDecoration(
                border: InputBorder.none,
                hintText: "密码",
              ),
            ),
          ),
        ),
        new Positioned(
          height: ScreenUtil.instance.setWidth(150),
          width: ScreenUtil.screenWidthDp,
          top: ScreenUtil.instance.setWidth(600),
          child: new Center(
            child: new Container(
              color: Colors.yellow,
              width: ScreenUtil.instance.setWidth(200),
              height: ScreenUtil.instance.setWidth(120),
              child: new GestureDetector(
                onTap: () {
                  Map<String, String> params = new Map();
                  params["username"] = _controllerUsername.text;
                  params["password"] = MD5Util.generateMd5(
                      MD5Util.generateMd5(_controllerPassword.text));

                  FocusScope.of(context).requestFocus(FocusNode());
                  _controllerUsername.clear();
                  _controllerPassword.clear();

                  print(params["username"]);
                  print(params["password"]);
                  new LoginNetUtil().loginGet(params, (result) {
                    print(result);
                    Map jsonResult = json.decode(result);
                    Toast.toast(context, jsonResult["data"]["message"]);
                  });
                },
                child: new Center(
                  child: new Text(
                    "登录",
                    style: new TextStyle(
                      fontSize: ScreenUtil.instance.setSp(50),
                      color: Colors.green,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
